package com.itheima.health.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.health.dao.MemberDao;
import com.itheima.health.pojo.Member;
import com.itheima.health.service.MemberService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * 会员SERVICE实现类
 */
@Service(interfaceClass = MemberService.class)
@Slf4j
public class MemberServiceImpl implements MemberService {
    @Autowired
    private MemberDao memberDao;

    @Transactional
    @Override
    public Member addIfNotExist(Member member) {
        log.info("[会员-添加]data:{}",member);
        Member pojo = memberDao.selectPhoneNumber(member.getPhoneNumber());
        if(null != pojo) {
            //会员已添加，直接返回
            return pojo;
        }
        //调用DAO插入数据
        memberDao.insert(member);
        return memberDao.selectById(member.getId());
    }

    @Override
    public Member findById(Integer id) {

        return memberDao.selectById(id);
    }

    @Override
    public List<Integer> countByMonth(List<String> months) {
        List<Integer> resultLIst = new ArrayList<>();
        for (String month : months) {
            String endDate = month+".31";
            Long count = memberDao.countByRegTimeBefore(endDate);
            resultLIst.add(count.intValue());
        }
        return resultLIst;
    }

    @Override
    public List<Map<String, Object>> countByMemberSex() {
        List<Map<String, Object>> list = memberDao.countByMemberSex();
        for (Map<String, Object> stringObjectMap : list) {
            if (null==(stringObjectMap.get("name"))){
                stringObjectMap.put("name","其他");
            }
        }
        return list;
    }

    @Override
    public Long countByMemberByAgeZeroToEighteen(Date todayTime, Date eighteenYearsTime) {
        return memberDao.countByMemberByAgeZeroToEighteen(todayTime,eighteenYearsTime);
    }

    @Override
    public Long countByMemberByAgeEighteenToThirty(Date eighteenYearsTime, Date thirtyYearsTime) {
        return memberDao.countByMemberByAgeEighteenToThirty(eighteenYearsTime,thirtyYearsTime);
    }

    @Override
    public Long countByMemberByAgeThirtyToForty_fiveYears(Date thirtyYearsTime, Date forty_fiveYearsTime) {
        return memberDao.countByMemberByAgeThirtyToForty_fiveYears(thirtyYearsTime,forty_fiveYearsTime);
    }

    @Override
    public Long countByMemberByAgeForty_fiveYearsToFuture(Date forty_fiveYearsTime) {
        return memberDao.countByMemberByAgeForty_fiveYearsToFuture(forty_fiveYearsTime);
    }
}
